package com.anhry.app.safety.manage.system.struts.action;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.system.struts.base.SystemBaseAction;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.bean.Permission;

/**
 * @date:2012-6-8
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
public class DeptAction extends SystemBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public List deptList;
	public List hyList;
	public String cType;//1:安监局2：属地3：行业
	public String sysType;
	public String deptName;//单位名称
	public String deptParentId;
	public String deptParentName;
	public String deptCode;//国标编码
	public String deptType;//部门类型1：科室2：行业3：属地
	public String deptGrade;//部门类型针对科室，行业： 1：省 2：市 3：县(市、区)、开发区
	public String deptClass;//部门类型：科室1:安监局2：普通科室3：行业监管 属地：1：安全科（镇级直管）2：非安全科（村）
	public String deptZg;//是否直管部门0：不直管2：直管 针对行业部门而言，安监局都是不直管的
	public String deptSj;//上级部门县(市、区)、开发区安监局，行业的上级部门的id
	public String id;
	public String pid;
	public TbUserDept dept;
	public String deptId;
	
	/**
	 * 所有部门列表
	 * @return
	 */
	@Permission(module = "dept", permissionVaule = 1)
	public String list(){
		try {
			if(null==this.cType || "".equals(this.cType)){
				this.setCType("1");
			}
			this.setDeptList(this.systemService.getDeptList(this.cType,Util.DEPT_CODE));
			if("1".equals(this.cType)){return "ks_list";}
			if("2".equals(this.cType)){return "hy_list";}
			if("3".equals(this.cType)){
				TbUserDept dept = this.systemService.getDeptByCode(Util.DEPT_CODE);
				this.setSysType(dept.getId().toString());
				return "sd_list";
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction deptList() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 跳转到部门编辑页面
	 * @return
	 */
	public String edit(){
		try {
			if("1".equals(this.cType)){return this.ksEdit();}
			if("2".equals(this.cType)){return this.hyEdit();}
			if("3".equals(this.cType)){return this.sdEdit();}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction edit() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	public String ksEdit(){
		try {
			if(null!=this.id && this.id.length()>0){//修改
				TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.id));
				this.setDept(dept);
			}
			if(null!=this.pid && this.pid.length()>0){//添加
				TbUserDept pdept = null;
				if("1".equals(this.pid)){//市级顶层安监局添加
					pdept = this.systemService.getDeptByCode(Util.DEPT_CODE);
				}else{
					pdept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.pid));
				}
				TbUserDept dept = new TbUserDept();
				dept.setDeptParentId(pdept.getId());
				dept.setDeptParentName(pdept.getDeptName());
				this.setDept(dept);
			}
			return "edit";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction ksEdit() 异常："+e.getMessage());
		}
		return ERROR;
	}
	public String sdEdit(){
		try {
			if(null!=this.id && this.id.length()>0){//修改
				TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.id));
				this.setDept(dept);
			}
			if(null!=this.pid && this.pid.length()>0){//添加
				TbUserDept pdept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.pid));
				TbUserDept dept = new TbUserDept();
				dept.setDeptParentId(pdept.getId());
				dept.setDeptParentName(pdept.getDeptName());
				this.setDept(dept);
			}
			return "edit";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction sdEdit() 异常："+e.getMessage());
		}
		return ERROR;
	}
	public String hyEdit(){
		try {
			this.setHyList((List)this.utilService.getHqlQuery("from TbUserDept t where 1=1 and t.deptType=2 and t.deptGrade=2 and t.isDele=0 order by t.id"));
			if(null!=this.id && this.id.length()>0){//修改
				TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.id));
				this.setDept(dept);
			}
			if(null!=this.pid && this.pid.length()>0){//添加
				TbUserDept pdept = null;
				if("1".equals(this.pid)){//市级顶层局添加
					pdept = this.systemService.getDeptByCode(Util.DEPT_CODE);
				}else{
					pdept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.pid));
				}
				TbUserDept dept = new TbUserDept();
				dept.setDeptParentId(pdept.getId());
				dept.setDeptParentName(pdept.getDeptName());
				this.setDept(dept);
			}
			return "edit";
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction hyEdit() 异常："+e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 部门信息保存
	 * @return
	 */
	public String save(){
		try {
			if(null!=this.deptId && this.deptId.length()>0){
				TbUserDept dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.deptId));
				if(null!=dept){
					dept.setDeptClass(this.deptClass);
					dept.setDeptCode(this.deptCode);
					dept.setDeptName(this.deptName);
					if(this.deptSj != null && !"".equals(deptSj.trim())) {
						dept.setDeptSj(Long.parseLong(this.deptSj));
					}
					this.utilService.update(dept);
					this.setTip("修改成功！");
					//将该部门下所有一级子菜单的父部门名称修改为当前部门名称
					this.utilService.deleteBySql("update tb_user_dept t set t.dept_parent_name='"+this.deptName+"' where t.dept_parent_id="+dept.getId());
				}
			}else{
				TbUserDept dept = new TbUserDept();
				dept.setDeptClass(this.deptClass);
				dept.setDeptCode(this.deptCode);
				dept.setDeptGrade(this.deptGrade);
				dept.setDeptName(this.deptName);
				dept.setDeptParentId(Long.parseLong(this.deptParentId));
				dept.setDeptParentName(this.deptParentName);
				dept.setDeptType(this.cType);
				if(this.deptSj != null) {
					dept.setDeptSj(Long.parseLong(this.deptSj));
				}
				dept.setDeptZg("0");
				dept.setIsDele("0");
				this.utilService.saveOrUpdate(dept);
				this.setTip("添加成功！");
			}
			return this.list();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("DeptAction save() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 部门删除
	 * @return
	 */
	public String delete(){
		try {
			if(this.getId() != null && !"".equals(this.getId())){
				TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
				TbUserDept pdept = (TbUserDept) this.utilService.getObjectById(TbUserDept.class, Long.parseLong(id));
				if(pdept != null){
					StringBuffer sb = new StringBuffer();	//拼接修改的hql语句,in(1,2,3)
					sb.append(pdept.getId()+",");
					List list = (List) this.systemService.getChildDeptList(pdept);
					if(list != null && list.size() > 0){
						for (int i = 0; i < list.size(); i++) {
							TbUserDept dept = (TbUserDept)list.get(i);
							sb.append(dept.getId()+",");
						}
					}
					if(sb != null && sb.length() > 0){
						sb.setLength(sb.length()-1);
						this.utilService.deleteBySql("update tb_user_dept t set t.is_dele=1 where t.id in("+sb.toString()+")");
						this.setTip("删除成功!");
						Loggers.info(user.getUserName()+" 用户，删除"+ sb +"部门信息！"+new Date());
					}
				}
			}
		} catch (Exception e) {
			Loggers.error("DeptAction:delete()方法出错："+e.getMessage());
			e.printStackTrace();
		}
		return this.list();
	}
	
	public String getCType() {
		return cType;
	}

	public void setCType(String type) {
		cType = type;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public String getSysType() {
		return sysType;
	}

	public void setSysType(String sysType) {
		this.sysType = sysType;
	}

	public String getDeptClass() {
		return deptClass;
	}

	public void setDeptClass(String deptClass) {
		this.deptClass = deptClass;
	}

	public String getDeptCode() {
		return deptCode;
	}

	public void setDeptCode(String deptCode) {
		this.deptCode = deptCode;
	}

	public String getDeptGrade() {
		return deptGrade;
	}

	public void setDeptGrade(String deptGrade) {
		this.deptGrade = deptGrade;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}

	public String getDeptZg() {
		return deptZg;
	}

	public void setDeptZg(String deptZg) {
		this.deptZg = deptZg;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public TbUserDept getDept() {
		return dept;
	}

	public void setDept(TbUserDept dept) {
		this.dept = dept;
	}

	public String getDeptParentId() {
		return deptParentId;
	}

	public void setDeptParentId(String deptParentId) {
		this.deptParentId = deptParentId;
	}

	public String getDeptParentName() {
		return deptParentName;
	}

	public void setDeptParentName(String deptParentName) {
		this.deptParentName = deptParentName;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDeptSj() {
		return deptSj;
	}

	public void setDeptSj(String deptSj) {
		this.deptSj = deptSj;
	}

	public List getHyList() {
		return hyList;
	}

	public void setHyList(List hyList) {
		this.hyList = hyList;
	}

}
